package com.situ.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.common.ServerResponse;
import com.situ.web.entity.Banji;
import com.situ.web.entity.Course;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Province;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.BanjiServiceImpl;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.JSONUtil;

import net.sf.json.JSONArray;

@WebServlet("/banji")
public class BanjiServlet extends HttpServlet {
	
	private IBanjiService banjiService = new BanjiServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		if (null == method || "".equals(method)) {
			method = "selectAll";
		}

		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		case "toUpdate":
			toUpdate(req, resp);
			break;
		case "Update":
			Update(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "checkName":
			checkName(req, resp);
			break;
		case "getInsertPage":
			getInsertPage(req, resp);
			break;	
		case "selectBanjiCourseInfo":
			selectBanjiCourseInfo(req, resp);
			break;
		case "selectClass":
			selectClass(req, resp);
			break;
		case "selectCourse":
			selectCourse(req, resp);
			break;
		case "chooseCourse":
			chooseCourse(req, resp);
			break;
		default:
			break;
		}
	}


	private void getInsertPage(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void checkName(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (null == pageNo || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (null == pageSize || "".equals(pageSize)) {
			pageSize = "3";
		}
		
		PageBean pageBean = banjiService.pageList(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		
		req.setAttribute("pageBean",pageBean);
		req.getRequestDispatcher("banji_page_list.jsp").forward(req, resp);
		
	}

	private void Update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String updateId = req.getParameter("id");
		String name = req.getParameter("name");

		Banji banji = new Banji(Integer.parseInt(updateId), name);
		boolean isSuccess = banjiService.update(banji);

		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String updateId = req.getParameter("id");

		Banji banji = banjiService.selectById(Integer.parseInt(updateId));
		// 放到一块内存区域里面
		req.setAttribute("banji", banji);
		// 请求转发到banji_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String searchName = req.getParameter("searchName");

		List<Banji> list = banjiService.search(searchName);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("UTF-8");
		String name = req.getParameter("name");
		
		Banji banji = new Banji(name);
		boolean isSuccess = banjiService.insert(banji);
		
		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("BanjiDeleteServlet.service()");
		String id = req.getParameter("id");

		boolean isSuccess = banjiService.deleteById(Integer.parseInt(id));
		// 删除完之后重定向到列表界面再显示所有数据
		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			List<Banji> list = banjiService.selectAll(); 
//			 放到一块内存区域里面
			req.setAttribute("list", list);
//			 请求转发到banji_list.jsp页面进行展示
			req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
			
	}
	
	private void selectBanjiCourseInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, Object>> list = banjiService.selectBanjiCourseInfo();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_course_list.jsp").forward(req, resp);
	}
	
	private void selectClass(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Banji> list = banjiService.selectAll(); 
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}
	
	private void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Course> list = banjiService.selectCourse(); 
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}
	
	private void chooseCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String classId = req.getParameter("classId");
		String courseId = req.getParameter("courseId");
		
		boolean isSuccess = banjiService.chooseCourse(Integer.parseInt(classId),Integer.parseInt(courseId));
		
		ServerResponse serverResponse = null;
		if (isSuccess) {
			serverResponse = ServerResponse.createSuccess("选课成功");
		} else {
			serverResponse = ServerResponse.createSuccess("已选该课程，选课失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
	}
}
